# coding:utf-8

import util;
import dervice;
import Clock;
import DomainFeatures;
import CapabilitiesParse;

class Guest:
    
    def __init__(self, type, name, uuid, memory, vcpus, isImport, imgPath, isoPath, mac, port, listen, caps):
        self.type = type;
        self.name = name;
        self.uuid = uuid;
        self.memory = memory;
        self.vcpus = vcpus;
        self.mac = mac;
        self.port = port;
        self.listen = listen;
        self.caps = caps;
        
        self.emulator = None;
        for d in self.caps.current:
            if d.type == type:
                self.emulator = d.emulator;
                break;
        
        self.device = dervice.Device(self.emulator, isImport, imgPath, isoPath,
                                      mac, port, listen);
        
        self.os = dervice.Installer(self.caps, "x86_64", "kvm");
        
        self.features = DomainFeatures.DomainFeatures()
    
    def get_xml_config(self):
        
        xml = "<domain type='%s'>\n" % self.type;
        
        name = "    <name>%s</name>\n" % self.name;        
        xml = util.xml_append(xml, name);
        
        uuid = "    <uuid>%s</uuid>\n" % self.uuid;
        xml = util.xml_append(xml, uuid);
        
        memory = "    <memory>%s</memory>\n" % self.memory;
        xml = util.xml_append(xml, memory);
        
        currentmemory = "    <currentMemory>%s</currentMemory>\n" % self.memory;
        xml = util.xml_append(xml, currentmemory);
        
        vcpus = "    <vcpu>%s</vcpu>\n" % self.vcpus;
        xml = util.xml_append(xml, vcpus);
        
        os = self.os.get_xml_config();        
        xml = util.xml_append(xml, os + "\n");
        
        features = self.features.get_xml_config();
        xml = util.xml_append(xml, features + "\n");
        
        clock = "    <clock offset='localtime' />\n";
        xml = util.xml_append(xml, clock);
        
        xml = util.xml_append(xml, "    <on_poweroff>destroy</on_poweroff>\n");
        xml = util.xml_append(xml, "    <on_reboot>destroy</on_reboot>\n");
        xml = util.xml_append(xml, "    <on_crash>destroy</on_crash>\n");
        
        devices = self.device.get_xml_config();
        xml = util.xml_append(xml, str(devices) + "\n");        
        
        xml = util.xml_append(xml, "</domain>");
        
        return xml;
